package se.jocke.nb.log.internal;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Collections2;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.JLabel;
import org.openide.util.lookup.ServiceProvider;
import se.jocke.nb.log.LogViewFilterManager;
import se.jocke.nb.log.model.LogViewFilter;

@ServiceProvider(service = LogViewFilterManager.class)
public class LogViewFilterManagerImpl implements LogViewFilterManager {

    public static final String FONT = ".font";

    public static final String FGCOLOR = ".fgcolor";

    public static final String BGCOLOR = ".bgcolor";

    public static final String PATTERN = ".pattern";

    public static final String WEIGHT = ".weight";

    public static final String REGEX = ".regex";

    public static final String FILTERS = "filters";

    private static final Preferences prefs = Preferences.userNodeForPackage(LogViewFilterManagerImpl.class);

    @Override
    public void store(List<LogViewFilter> filters) {
        List<LogViewFilter> copy = new ArrayList<LogViewFilter>(filters);
        Collections.sort(copy);
        String names = Joiner.on(",").join(Collections2.transform(copy, new Function<LogViewFilter, String>() {
            @Override
            public String apply(LogViewFilter f) {
                return f.getName();
            }
        }));
        prefs.put(FILTERS, names);
        for (LogViewFilter logViewFilter : copy) {
            prefs.put(logViewFilter.getName() + PATTERN, logViewFilter.getPattern());
            prefs.put(logViewFilter.getName() + BGCOLOR, colorToHex(logViewFilter.getBackgroundColor()));
            prefs.put(logViewFilter.getName() + FGCOLOR, colorToHex(logViewFilter.getForegroundColor()));
            prefs.put(logViewFilter.getName() + FONT, logViewFilter.getFont().getName());
            prefs.put(logViewFilter.getName() + WEIGHT, Integer.toString(logViewFilter.getWeight()));
            prefs.putBoolean(logViewFilter.getName() + WEIGHT, logViewFilter.isRegex());
        }
    }

    @Override
    public List<LogViewFilter> findAll() {
        List<LogViewFilter> filters = new ArrayList<LogViewFilter>();

        for (String filter : Splitter.on(",").omitEmptyStrings().trimResults().split(prefs.get(FILTERS, LogViewFilter.DEFAULT_FILTER_NAME))) {
            LogViewFilter logViewFilter = new LogViewFilter();
            logViewFilter.setName(filter);
            if (LogViewFilter.DEFAULT_FILTER_NAME.equals(filter)) {
                logViewFilter.setWeight(Integer.MAX_VALUE);
            } else {
                logViewFilter.setWeight(prefs.getInt(filter + WEIGHT, 0));
            }
            logViewFilter.setBackgroundColor(Color.decode(prefs.get(filter + BGCOLOR, colorToHex(Color.WHITE))));
            logViewFilter.setForegroundColor(Color.decode(prefs.get(filter + FGCOLOR, colorToHex(Color.BLACK))));
            logViewFilter.setFont(Font.decode(prefs.get(filter + FONT, new JLabel().getFont().getName())));
            logViewFilter.setPattern(prefs.get(filter + PATTERN, ".*"));
            logViewFilter.setRegex(prefs.getBoolean(filter + REGEX, Boolean.FALSE));
            filters.add(logViewFilter);
        }

        Collections.sort(filters);

        return filters;
    }

    private static String colorToHex(Color color) {
        return "#" + Integer.toHexString(color.getRGB()).substring(2);
    }

}
